package com.shuqi.activity.adapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.shuqi.controller.R;
import com.shuqi.database.model.BookCataLog;
import com.shuqi.database.model.BookInfo;

/**
 * 收费书籍目录adapter
 * 
 * @author yhw
 */
public class BookdetailsCatalogAdapter2 extends BaseExpandableListAdapter {
    private LayoutInflater inflater;
    private List<BookCataLog> list;

    private List<BookCataLog> vList = new ArrayList<BookCataLog>(); // 卷集合
    private Map<String, List<BookCataLog>> cMap = new HashMap<String, List<BookCataLog>>(); // 章集合

    private String mSelectorChapterId;
    private int mSelectorTextFontColor = 0xff14be7d;
    private int mTextFontColor = 0xFF000000;
    private int mSelectorGroupPosition;
    private int mSelectorChildposition;

    public BookdetailsCatalogAdapter2(Context context, List<BookCataLog> list) {
        this.inflater = LayoutInflater.from(context);
        this.list = list;
        initData();
    }

    /**
     * 初始化卷和章信息
     */
    private void initData() {
        if (list != null) {
            vList.clear();
            cMap.clear();
            for (BookCataLog bookCatalog : list) {
                if (bookCatalog.getChapterState() == BookCataLog.UN_IS_CHATPTER) {
                    vList.add(bookCatalog);
                } else {
                    String vid = bookCatalog.getVolumeId();
                    List<BookCataLog> chapteList = cMap.get(vid);
                    if (chapteList == null) {
                        chapteList = new ArrayList<BookCataLog>();
                        cMap.put(vid, chapteList);
                    }
                    chapteList.add(bookCatalog);
                }
            }
        }
    }


    /**
     * 更新数据
     * 
     * @param mCatalog 章节数据
     */
    public void setList(List<BookCataLog> list) {
        this.list = list;
        initData();
    }

    private static class Holder {
        public TextView tv1;
        public ImageView img1;
    }

    @Override
    public int getGroupCount() {
        if (vList != null) {
            return vList.size();
        }
        return 0;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        if (vList != null && groupPosition >= 0 && groupPosition < vList.size()) {
            String vid = vList.get(groupPosition).getChapterId();
            if (vid != null && cMap.get(vid) != null) {
                return cMap.get(vid).size();
            }
        }
        return 0;
    }

    @Override
    public Object getGroup(int groupPosition) {
        return vList.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        String vid = vList.get(groupPosition).getChapterId();
        if (vid != null) {
            return cMap.get(vid).get(childPosition);
        }
        return null;
    }

    @Override
    public long getGroupId(int groupPosition) {
        return 0;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return 0;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView,
            ViewGroup parent) {
        BookCataLog catalog = vList.get(groupPosition);
        View view = null;
        if (convertView != null) {
            view = convertView;
        } else {
            view = inflater.inflate(R.layout.volume_list_item, parent, false);
        }
        Holder holder = (Holder) view.getTag();
        if (holder == null) {
            holder = new Holder();
            holder.tv1 = (TextView) view.findViewById(R.id.textView1);
            holder.img1 = (ImageView) view.findViewById(R.id.imgView_itemlayout_listview3);
            view.setTag(holder);
        }
        holder.tv1.setText(catalog.getChapterName());
        if (!isExpanded) {
            holder.img1.setImageResource(R.drawable.unexpanded);
        } else {
            holder.img1.setImageResource(R.drawable.expanded);
        }
        return view;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild,
            View convertView, ViewGroup parent) {
        View view = null;
        if (convertView != null) {
            view = convertView;
        } else {
            view = inflater.inflate(R.layout.chapter_list_item, parent, false);
        }
        Holder holder = (Holder) view.getTag();
        if (holder == null) {
            holder = new Holder();
            holder.tv1 = (TextView) view.findViewById(R.id.textView1);
            holder.img1 = (ImageView) view.findViewById(R.id.imgView_itemlayout_listview3);
            view.setTag(holder);
        }

        String vid = vList.get(groupPosition).getChapterId();
        BookCataLog catalog = cMap.get(vid).get(childPosition);

        holder.tv1.setText(catalog.getChapterName());
        if (mSelectorChapterId != null && catalog.getChapterId().equals(mSelectorChapterId)) {
            holder.img1.setVisibility(View.VISIBLE);
            holder.img1.setImageResource(R.drawable.icon__catalog_bookmark);
            holder.tv1.setTextColor(mSelectorTextFontColor);
        } else {
            int payMode = catalog.getPayMode();
            if (payMode == BookInfo.ALL_BOOK || payMode == BookInfo.ALL_CHAPTER) {
                if (catalog.getPayState() == BookCataLog.NO_PAY) {
                    holder.img1.setVisibility(View.VISIBLE);
                    holder.img1.setImageResource(R.drawable.icon_bookditeails_catalog_buy0);
                } else {
                    holder.img1.setVisibility(View.GONE);
                }
            } else {
                holder.img1.setVisibility(View.GONE);
            }
            holder.tv1.setTextColor(mTextFontColor);
        }

        return view;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    public BookCataLog getInfoFromCataLog(int groupPosition, int childPosition) {
        BookCataLog group = vList.get(groupPosition);
        if (group != null) {
            String vid = group.getChapterId();
            List<BookCataLog> childList = cMap.get(vid);
            if (childList != null) {
                BookCataLog catalog = childList.get(childPosition);
                return catalog;
            }
        }
        return null;
    }

    public void setSelectorChapterId(String chapterId) {
        mSelectorChapterId = chapterId;
    }

    public int[] getGroupAndChildPosition(String chapterId) {
        int[] result = new int[2];
        mSelectorGroupPosition = 0;
        mSelectorChildposition = 0;
        if (chapterId != null) {
            breakTo: for (BookCataLog groupCatalog : vList) {
                String vid = groupCatalog.getChapterId();
                List<BookCataLog> childList = cMap.get(vid);
                mSelectorChildposition = 0;
                if(childList == null){
                    ++mSelectorGroupPosition;
                    continue;
                }
                for (BookCataLog catalog : childList) {
                    if (catalog.getChapterId().equals(chapterId)) {
                        break breakTo;
                    }
                    ++mSelectorChildposition;
                }
                ++mSelectorGroupPosition;
            }
        }
        result[0] = mSelectorGroupPosition;
        result[1] = mSelectorChildposition;
        return result;
    }
}
